using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using 畜牧业审核程序国产.ViewModels;

namespace 畜牧业审核程序国产;

public partial class ChatWindow : Window
{
    public ChatWindow(string apiKey,string apiUrl,string ragModel)
    {
        InitializeComponent();
        this.ChatShowCotrolScrollViewer.ScrollToEnd();
        // 创建 ChatWindowViewModel 并传递参数
        var chatWindowViewModel = new ChatWindowViewModel(apiKey, apiUrl, ragModel);

        // 设置 DataContext
        this.DataContext = chatWindowViewModel;
        //this.ChatShowControl.PropertyChanged += ChatShowControl_PropertyChanged; 
        //this.ChatShowControl.ResourcesChanged += (sendor, e) => this.ChatShowCotrolScrollViewer.ScrollToEnd();
        this.ChatShowControl.SizeChanged += (sendor, e) => { this.ChatShowCotrolScrollViewer.ScrollToEnd();};
        //this.ChatShowControl.EffectiveViewportChanged += (sendor, e) => this.ChatShowCotrolScrollViewer.ScrollToEnd();
        //this.ChatShowControl.Tapped += (sendor, e) => this.ChatShowCotrolScrollViewer.ScrollToEnd();
        this.ChatShowCotrolScrollViewer.SizeChanged += (sendor, e) => this.ChatShowCotrolScrollViewer.ScrollToEnd();
        this.ChatShowCotrolScrollViewer.DoubleTapped += (sendor, e) => this.ChatShowCotrolScrollViewer.ScrollToEnd();
        var viewmodel = (ChatWindowViewModel)this.DataContext;//先放着
    }
    public ChatWindow()
    {
            
    }
    //public ChatWindow()
    //{
    //    InitializeComponent();
    //    this.ChatShowCotrolScrollViewer.ScrollToEnd();
    //    //this.ChatShowControl.PropertyChanged += ChatShowControl_PropertyChanged; 
    //    //this.ChatShowControl.ResourcesChanged += (sendor, e) => this.ChatShowCotrolScrollViewer.ScrollToEnd();
    //    this.ChatShowControl.SizeChanged += (sendor, e) => this.ChatShowCotrolScrollViewer.ScrollToEnd();
    //}


    private void TextBox_KeyDown(object? sender, Avalonia.Input.KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            var viewModel = DataContext as ChatWindowViewModel;
            if (viewModel != null && viewModel.SendMessageCommand.CanExecute(null))
            {
                viewModel.SendMessageCommand.Execute(null);
               // this.progressBar.IsActive = true;
            }
        }
    }
    // 确保每次ItemsSource更新后，ScrollViewer滚动到最底部
    private void ChatShowControl_PropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
    {
        if (e.Property.Name == "MessagesInChatWindow")
        {
            this.ChatShowCotrolScrollViewer.ScrollToEnd();
        }
    }
}